import React, { ChangeEventHandler, useEffect } from "react";

import { find, add, deleteApi, update } from "@/api/user";

import { findPopulate, addPopulate } from "@/api/order";

import {
  uploadFile,
  findFile,
  existFile,
  fileDelete,
  getFileFromA
} from "@/api/file";

import "./index.scss";

export default (): JSX.Element => {
  const init = async () => {
    // 文件相关
    // const data = await findFile("60dc286f6983fe6798d28742");
    // getFileFromA(data.arr, data.name)

    // 存储数据相关
    // const data = await add({
    //   title: "新增数据",
    //   description: "这是我的新增数据",
    //   by: "洪伟江",
    //   url: "http://www.洪伟江.com1",
    //   tags: ["新增数据", "洪伟江", "add"],
    //   likes: 100,
    // });
    // console.log(data)
    // const data = await deleteApi('605ffb371962c96f9461081e')
    // const data = await update("605ffb371962c96f9461081e", {
    //   title: "修改数据",
    //   description: "这是我的修改数据",
    //   by: "洪伟江改",
    //   url: "http://www.洪伟江.com",
    //   tags: ["修改数据", "洪伟江", "add"],
    //   likes: 100,
    // });
    // const list = await find("");
    // console.log(list);
    // 联表
    // const data = await addPopulate('60dc243a347def71282ebd7c', {
    //   name: '洪伟江联表1',
    //   money: 2001
    // })
    // const list = await findPopulate("60dc243a347def71282ebd7c")
    // console.log(list);
  };
  const change = async (e: React.ChangeEvent<HTMLInputElement>) => {
    if (!e.target.files) return;
    let formData = new FormData();
    formData.append("file", e.target.files[0]);
    uploadFile(formData, (data) => {
      console.log(data);
    }).then(async (res) => {
      const data = await findFile(res.id);
      console.log(data);
    });
  };
  useEffect(() => {
    init();
  }, []);
  return (
    <div className="mongo">
      <p>mongo</p>
      <input
        onChange={(e: React.ChangeEvent<HTMLInputElement>) => change(e)}
        type="file"
      />
    </div>
  );
};
